package com.wlyy.bcwlw.common.export2excel.impl;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.converters.StringConverter;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.Region;

import com.wlyy.bcwlw.common.export2excel.exception.ImpExpException;
import com.wlyy.bcwlw.common.export2excel.inter.IXyExport;
import com.wlyy.bcwlw.common.utils.ExcelUtil;

public class XyExportImpl implements IXyExport {
	
	private static int MAX_ROW_NUM = 60000;
	private static int DEFAULT_COlUMN_WIDTH = 10;

	public Object export(List header, List data,String title) throws ImpExpException {
		if (header == null || header.size() == 0)
			throw new ImpExpException("表头为空");
		if (data == null || data.size() == 0)
			throw new ImpExpException("数据为空");
		if (title == null)
			throw new ImpExpException("内容为空");
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = null;
		Iterator it = data.iterator();
		int sheetIndex = 0;
		int i = 0;
		boolean hasTitle = false;
		while (it.hasNext()) {
			int row = i%MAX_ROW_NUM;
			if(row==0){
				sheet = wb.createSheet();
				wb.setSheetName(sheetIndex, (i+1)+"-"+(i+MAX_ROW_NUM));
				sheetIndex++;
				sheet.setDefaultColumnWidth(DEFAULT_COlUMN_WIDTH);
				if(StringUtils.isNotBlank(title)){
					hasTitle = true;
					setTitleRow(wb,sheet, header, title, 0);
					setHeaderRow(wb,sheet, header, 1);
				}else {
					setHeaderRow(wb,sheet, header, 0);
				}
				
			}
			if (hasTitle) {
				setRow(wb,sheet, (List) it.next(), row+2);
			} else {
				setRow(wb,sheet, (List) it.next(), row+1);
			}
			
			i++;
		}
		return wb;
	}	
	
	
	private void setTitleRow(HSSFWorkbook wb,HSSFSheet sheet,List header,String title, int row) {
		Iterator it = header.iterator();
		int col = 0;
		HSSFCell cell = null;
		while (it.hasNext()) {
			it.next();
			cell = ExcelUtil.getCell(sheet, row, col++);
		}						
		sheet.addMergedRegion(new CellRangeAddress(0,(short)0,0,(short)(header.size()-1)));
		HSSFRow row2 = sheet.getRow(0);
		cell =  row2.getCell(0);		
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER );
		HSSFFont font = wb.createFont();
		font.setFontHeight((short)350);
		font.setBoldweight((short)2000);
		style.setFont(font);
		cell.setCellStyle(style); 		
		ExcelUtil.setText(cell, title);
	}
	
	public static void setHeaderRow(HSSFWorkbook wb,HSSFSheet sheet, List data, int row) {
		Iterator it = data.iterator();
		int col = 0;
		HSSFCell cell = null;
		while (it.hasNext()) {
			cell = ExcelUtil.getCell(sheet, row, col++);
			Object item = it.next();
			String text = "";
			Converter converter = new StringConverter();
			text = (String) converter.convert(String.class, item);
			ExcelUtil.setText(cell,text);
			HSSFCellStyle style = wb.createCellStyle();
			style.setAlignment(HSSFCellStyle.ALIGN_CENTER );
			HSSFFont font = wb.createFont();
			font.setBoldweight((short)2000);
			style.setFont(font);
			cell.setCellStyle(style);
		}
	}
	
	public static void setRow(HSSFWorkbook wb,HSSFSheet sheet, List data, int row) {
		Iterator it = data.iterator();
		int col = 0;
		HSSFCell cell = null;
		while (it.hasNext()) {
			cell = ExcelUtil.getCell(sheet, row, col++);
			Object item = it.next();
			String text = "";
			Converter converter = new StringConverter();
			text = (String) converter.convert(String.class, item);
			ExcelUtil.setText(cell, text);
		}
	}
	public Object export(List header, List data) throws ImpExpException {
		return this.export(header, data, "");
	}	

}
